<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <link rel="stylesheet" href="/css/reset.css">
    <link rel="stylesheet" href="/css/login.css">
    <script src="http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
    <title>登录</title>
</head>
<body>
    <div class="container">
        <div class="sign_container">
            <h1 class="title">后台管理系统</h1>
            <div class="login_content">
                <form id="form1" method="post" onsubmit="return false" action="##">
                    <div class="sign_account">
                        <div class="ipt_box">
                            <input name="username" type="text" class="ipt" placeholder="手机号或邮箱" value="">
                        </div>
                    </div>
                    <div class="sign_account">
                        <div class="ipt_box">
                            <input name="password" type="text" class="ipt" placeholder="输入密码！" value="">
                        </div>
                    </div>
                    <div class="submit_box">
                        <button onclick="login()">登录</button>
                    </div>
                </form>
            </div>
            
            <div class="footer">
                没有帐号？<span>注册</span>
            </div>
        </div>
        
    </div>
    <script>
        function login() {
            console.log($('#form1').serialize());
            $.ajax({
                type: "POST",//方法类型
                dataType: "json",//预期服务器返回的数据类型
                url: "/api/admin/login" ,//url
                data: $('#form1').serialize(),
                success: function (result) {
                    console.log(result);//打印服务端返回的数据(调试用)
                    if (result.status == 'success') {
                        var user = result.data;
                        if(user.role === 'user') {
                            window.location.href = '/';
                        } else {
                            window.location.href = '/admin';
                        }
                        
                    }
                },
                error : function() {
                    alert("异常！");
                }
            });
        }
        
    </script>
</body>
</html>